package com.xnx3.wangmarket.plugin.innerDown.generateCache;

import java.util.List;
import org.springframework.stereotype.Component;
import com.xnx3.j2ee.func.Log;
import com.xnx3.j2ee.generateCache.BaseGenerate;
import com.xnx3.wangmarket.domain.G;
import com.xnx3.wangmarket.plugin.innerDown.entity.InnerDownBranch;

/**
 * 部门信息缓存
 * @author 管雷鸣
 *
 */
@Component
public class Branch extends BaseGenerate{
	
	//运行项目时自动执行，生成缓存
	public Branch() {
		new Thread(new Runnable() {
			public void run() {
				boolean b = true;
				while(b){
					try {
						//延迟 10 秒
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(G.getDomainSize() > 0){
						b = false;
					}else{
						//项目中的二级域名个数是0，还没有加载域名，等待加载完域名数据后，在进行加载这里的数据
					}
				}
				
				List<InnerDownBranch> list = com.xnx3.j2ee.func.Sql.getSqlService().findBySqlQuery("SELECT * FROM plugin_innerdown_branch WHERE isdelete = "+InnerDownBranch.ISDELETE_NORMAL, InnerDownBranch.class);
				Log.info("plugin_innerDown load branch "+list.size()+" number");
				branchid(list);
				
			}
		}).start();
	}
	
	
	/**
	 * 生成部门的 id-name 的js缓存<br/>
	 * 1. 当运行项目时生成<br/>
	 * 2. 当修改部门时生成
	 * @param list sqlservice 的查询结果
	 */
	public void branchid(List<InnerDownBranch> list){
		createCacheObject("branchid");
		for (int i = 0; i < list.size(); i++) {
			InnerDownBranch branch = list.get(i);
			cacheAdd(branch.getId(), branch.getName());
		}
		generateCacheFile();
	}
	
}
